package com.qing.expert.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qing.expert.common.exception.BusinessException;
import com.qing.expert.modules.expert.mapper.ExpertApplyMapper;
import com.qing.expert.modules.admin.service.AdminExpertService;
import com.qing.expert.modules.expert.entity.ExpertApply;
import com.qing.expert.modules.user.entity.User;
import com.qing.expert.modules.user.mapper.UserMapper;
import com.qing.expert.modules.expert.entity.Expert;
import com.qing.expert.modules.expert.mapper.ExpertMapper;
import com.qing.expert.modules.expert.mapper.ExpertApplyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class AdminExpertServiceImpl implements AdminExpertService {

    @Resource
    private ExpertApplyMapper expertApplyMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ExpertMapper expertMapper;

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public ExpertApply getExpertApply(String applyId) {
        ExpertApply apply = expertApplyMapper.selectById(applyId);
        if (apply == null) {
            throw new BusinessException("申请记录不存在");
        }
        return apply;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditExpertApply(String applyId, Integer action) {
        // 查询申请记录
        ExpertApply apply = expertApplyMapper.selectById(applyId);

        if (apply == null) {
            throw new BusinessException("申请记录不存在");
        }

        // 检查申请状态
        if (apply.getStatus() != 0) {
            throw new BusinessException("该申请已处理");
        }

        // 更新申请记录
        apply.setStatus(action);
        expertApplyMapper.updateById(apply);

        // 如果审核通过
        if (action == 1) {
            // 1. 更新用户角色为达人
            User user = userMapper.selectById(apply.getUserId());
            if (user == null) {
                throw new BusinessException("用户不存在");
            }
            user.setRole(2); // 2=达人角色
            userMapper.updateById(user);

            // 2. 创建达人信息
            Expert expert = new Expert()
                    .setExpertId("E" + IdUtil.getSnowflakeNextIdStr())
                    .setUserId(apply.getUserId())
                    .setCategoryId(apply.getCategoryId())
                    .setDescription(apply.getDescription())
                    .setCertificateImages(apply.getCertificateImages())
                    .setRating(5.0)
                    .setCompletedOrders(0)
                    .setIsBusy(false)
                    .setStatus(1);

            expertMapper.insert(expert);

            log.info("用户[{}]成功成为达人[{}]", user.getUserId(), expert.getExpertId());
        } else {
            log.info("拒绝用户[{}]的达人申请", apply.getUserId());
        }

        log.info("达人申请[{}]审核完成，结果：{}", applyId, action == 1 ? "通过" : "拒绝");
    }
}